﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Typing_GUI
{
    public partial class frmEditAdventureLesson : Form
    {
        public frmEditAdventureLesson()
        {
            InitializeComponent();
        }
        public CLASS.CBaiTap bt;
        public bool updated=false;
        public int UserId;
        public string username = "", levelLesson = "";
        void LoadLevel()
        {
            string sql = "select*from CapDoBaiTap";
            DataTable dt = CLASS.DataProvider.ExecuteQuery(sql);
            combLevel.DataSource = dt;
            combLevel.DisplayMember = "TenCapDo";
            combLevel.ValueMember = "MaCapDoBaiTap";
        }

        void LoadUser()
        {
            string sql = "select tendangnhap,manguoidung from nguoidung";
            DataTable dt = CLASS.DataProvider.ExecuteQuery(sql);
            cobUser.DataSource = dt;
            cobUser.DisplayMember = "tendangnhap";
            cobUser.ValueMember = "Manguoidung";
        }

        void LoadAvatar_Level()
        {
            string un = CLASS.CNguoiDung.SetAvatar_Level(UserId, pbAvatar, pbLevel);
            lblUsername.Text = un;
            ToolTip toolTip1 = new ToolTip();
            toolTip1.SetToolTip(pbLevel, pbLevel.Tag.ToString());
        }

        private void frmEditAdventureLesson_Load(object sender, EventArgs e)
        {
            LoadLevel();
            LoadUser();
            txtLessonName.Text = bt.Tenbaitap;
            txtTime.Text = bt.Thoigian.ToString();
            txtMaxScore.Text = bt.Diemtoida.ToString();
            txtDescription.Text = bt.Mota;
            cobUser.SelectedValue = bt.Nguoisoan.ToString();
            combLevel.SelectedValue = bt.Capdobaitap.ToString();
            chbState.Checked = bt.TinhTrang;
            string[] chuoitu = bt.Noidung.Split(' ');
            for(int i=0;i<chuoitu.Length;i++)
                lbWordList.Items.Add(chuoitu[i]);
            LoadAvatar_Level();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lbWordList.SelectedIndices.Count > 0)
            {
                for (int i = lbWordList.SelectedIndices.Count - 1; i >= 0; i--)
                {
                    lbWordList.Items.RemoveAt(lbWordList.SelectedIndices[i]);
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClearField_Click(object sender, EventArgs e)
        {
            txtLessonName.Text = "";
            txtMaxScore.Text = "0";
            txtDescription.Text = "";
            combLevel.SelectedIndex = 0;
            txtTime.Text = "";
        }
        bool testword()
        {
            if (txtWord.Text == "" || txtWord.Text == " ")
                return false;
            for(int i=0;i<txtWord.Text.Length;i++)
                if (!char.IsLetter(txtWord.Text[i]))
                {
                    MessageBox.Show("Phai nhap toan chu.");
                    txtWord.Text = "";
                    txtWord.Focus();
                    return false;
                } 
            return true;
        }

        #region"KIEM TRA DU LIEU NHAP."
        bool kiemtradulieunhap()
        {
            ///kiem tra ten bai hoc.
            if (txtLessonName.Text == "")
            {
                MessageBox.Show("Lesson name not be blank!", "MEssage", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtLessonName.Focus();
                return false;
            }

            ///kiem tra thoi gian.
            if (txtTime.Text != "")
            {
                for (int i = 0; i < txtTime.Text.Length; i++)
                {
                    if (!char.IsNumber(txtTime.Text[i]))
                    {
                        MessageBox.Show("Time must be numeric!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtTime.Text = "";
                        txtTime.Focus();
                        return false;
                    }
                }
            }
            else
            {
                MessageBox.Show("Time not be blank!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtTime.Focus();
                return false;
            }

            ///kiem tra diem toi da.
            if (txtMaxScore.Text != "")
            {
                for (int i = 0; i < txtMaxScore.Text.Length; i++)
                {
                    if (!char.IsNumber(txtMaxScore.Text[i]))
                    {
                        MessageBox.Show("Max score must be numeric!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtMaxScore.Text = "";
                        txtMaxScore.Focus();
                        return false;
                    }
                }
            }
            else
            {
                MessageBox.Show("Max score  not be blank!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtMaxScore.Focus();
                return false;
            }
            ///kiem tra mo ta.
            if (txtDescription.Text == "")
            {
                MessageBox.Show("Description not be blank!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtDescription.Focus();
                return false;
            }

            ///kiem tra tu da co chua.
            if (lbWordList.Items.Count == 0)
            {
                MessageBox.Show("No word inputed!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtWord.Focus();
                return false;
            }
            return true;
        }
        #endregion

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (lbWordList.SelectedIndices.Count > 0)
            {
                if (testword())
                {
                    int vt = lbWordList.SelectedIndices[0];

                    lbWordList.Items[lbWordList.SelectedIndices[0]] = txtWord.Text;
                }
            }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            if (testword())
            {
                lbWordList.Items.Add(txtWord.Text);
            }
        }

        private void btnUpdateLesson_Click(object sender, EventArgs e)
        {
            if (kiemtradulieunhap())
            {
                //Tao chuoi tu./////////////////////////////////////////////////
                string tu = "";
                for (int i = 0; i < lbWordList.Items.Count - 1; i++)
                    tu += lbWordList.Items[i].ToString() + " ";
                tu += lbWordList.Items[lbWordList.Items.Count - 1].ToString();

                ////////////////////////////////////////////////////////////////
                
                bt.Tenbaitap = txtLessonName.Text;
                bt.Noidung = tu;
                bt.Mota = txtDescription.Text;
                bt.Thoigian = int.Parse(txtTime.Text);
                bt.Diemtoida = int.Parse(txtMaxScore.Text);
                bt.Capdobaitap = int.Parse(combLevel.SelectedValue.ToString());
                bt.Loaibaitap = 2;
                bt.Nguoisoan = int.Parse(cobUser.SelectedValue.ToString());
                bt.TinhTrang = chbState.Checked;
                if (bt.UpdateLesson())
                {
                    updated = true;
                    username = cobUser.Text;
                    levelLesson = combLevel.Text;
                    MessageBox.Show("The lesson is updated successful.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                } 
                else
                    MessageBox.Show("The lesson is update failed.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
            }
        }

        private void lbWordList_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtWord.Text = lbWordList.Text;
        }
    }
}